let Sequelize = require("sequelize");

module.exports = function(sequelize, DataTypes) {
	let UserProfile = sequelize.define("UserProfile", {
		id: { type: Sequelize.INTEGER, primaryKey: true,autoIncrement: true ,field: 'id',comment:'主键',unique: true},
		uid : {type: Sequelize.INTEGER,
			references: {
				model: 'User',
				key: 'id',
			}
		},
		from_uid : {type: Sequelize.INTEGER,references: {
			model: 'User',
			key: 'id'
		}},
		"cert_category": {type: Sequelize.STRING},
		"cert_front_img": {type: Sequelize.STRING},
		"cert_sn": {type: Sequelize.STRING},
		"cert_status": {type: Sequelize.STRING},
		"corp_id": {type: Sequelize.STRING},
		"email": {type: Sequelize.STRING},
		"extattr": {type: Sequelize.STRING},
		"idcard_back_img": {type: Sequelize.STRING},
		"idcard_front_img": {type: Sequelize.STRING},
		"idcard": {type: Sequelize.STRING},
		"idcard_status": {type: Sequelize.STRING},
		"register_origin": {type: Sequelize.STRING},
		"remark": {type: Sequelize.STRING},
		"role": {type: Sequelize.STRING},
		"subject": {type: Sequelize.STRING},
		"birthday": {type: Sequelize.DATE},
	}, {
		tableName: 'uc_user_profile',
		comment: '用户profile表',
		underscored: true,
		'timestamps': true,
		'paranoid': true, //查询并加载软删除的数据
		'createdAt' : 'created_at',
		'updatedAt' : 'updated_at',
		'deletedAt' : 'deleted_at',
		classMethods: {
		associate: function(models) {
			UserProfile.belongsTo(models.User, {
				onDelete: "CASCADE",
				foreignKey: {
					name : 'from_uid',
					allowNull: true
				}
			});
			models.User.hasMany(UserProfile,{foreignKey : 'from_uid',targetKey:'id'})
			UserProfile.belongsTo(models.User, {
				onDelete: "CASCADE",
				foreignKey: {
					name : 'uid',
					allowNull: false
				}
			});
			models.User.hasMany(UserProfile,{foreignKey : 'uid',targetKey:'id'})
		}
		}
	});
	return UserProfile;
};
